package com.shipxpress.register.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.shipxpress.SessionManager;
import com.shipxpress.register.model.User;
import com.shipxpress.register.model.UserList;
import com.shipxpress.register.view.UserForm;

public class UserRegistrationAction extends Action{
    @Override
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        if (isCancelled(request)) {
            return mapping.findForward("done");
        }
        //action forward
        if (request.getParameter("forwardAction").equalsIgnoreCase("done")) {
            return mapping.findForward("done");
        }

        //global forward
        if (request.getParameter("forwardAction").equalsIgnoreCase("index")) {
            return mapping.findForward("index");
        }

        //populate values to User and do business logic operations
        UserList userList = SessionManager.getSessionUserList(request.getSession());
        User user = new User();
        ((UserForm)form).updateData(user);
        user.doOperations(userList);
        SessionManager.setUserList(request.getSession(), userList);

        //after the operation forward to success page
        return mapping.findForward("success");
    }
}
